﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using FileHelper.FileStreaming;

namespace FileHelper
{
    public class FileStreamManager
    {
        
        //RemoteFileInfo DownloadFile(DownloadRequest filename);


        public static UploadedFileInfo UploadFile(RemoteFileInfo request)
        {
            StreamingServiceClient proxy = new StreamingServiceClient();
            FileStreaming.RemoteFileInfo file = new FileStreaming.RemoteFileInfo(request.FileName,request.FolderId,request.Length,request.Owner,request.FileByteStream);
            FileStreaming.UploadedFileInfo result = proxy.UploadFile(file);
            proxy.Close();
            return new UploadedFileInfo() {UploadedFileId = result.UploadedFileId,Path= result.Path};
        }


        public static bool Move(int fileId, int toFolderId)
        {
            StreamingServiceClient proxy = new StreamingServiceClient();            
            MoveResponse result = proxy.Move(new MoveRequest(fileId,toFolderId));
            proxy.Close();
            return result.MoveResult;
        }


        public static bool Copy(int fileId, int toFolderId)
        {
            StreamingServiceClient proxy = new StreamingServiceClient();
            CopyResponse result = proxy.Copy(new CopyRequest(fileId, toFolderId));
            proxy.Close();
            return result.CopyResult;
        }


        public static bool DeleteFile(int fileId)
        {
            StreamingServiceClient proxy = new StreamingServiceClient();
            DeleteFileResponse result = proxy.DeleteFile(new DeleteFileRequest(fileId));
            proxy.Close();
            return result.DeleteFileResult;
        }


        public static List<FileInfo> GetFiles(int folderId, int sortBy, bool isSortAscending, int pageSize, int pageIndex)
        {
            List<FileInfo> lst = new List<FileInfo>();
            StreamingServiceClient proxy = new StreamingServiceClient();
            GetFilesResponse result = proxy.GetFiles(new GetFilesRequest(folderId,sortBy,isSortAscending,pageSize,pageIndex));
            proxy.Close();
            foreach (File f in result.GetFilesResult)
            {
                lst.Add(new FileInfo() { Id = f.Id, Filename = f.Filename, FolderId = f.FolderId, LastModified = f.LastModified, Owner = f.Owner, Path = f.Path, Size = f.Size });
            }
            return lst;
        }


        public static int CreateFolder(FolderInfo objFolder)
        {
            StreamingServiceClient proxy = new StreamingServiceClient();
            CreateFolderRequest request =new CreateFolderRequest(new Folder(){Id=objFolder.Id, FolderName= objFolder.FolderName, IsPublic= objFolder.IsPublic, Owner = objFolder.Owner, ParentId = objFolder.ParentId, Path = objFolder.Path});
            CreateFolderResponse result = proxy.CreateFolder(request);
            proxy.Close();
            return result.CreateFolderResult;
        }

        public static bool ChangeName(int folderId, string newFolderName)
        {
            StreamingServiceClient proxy = new StreamingServiceClient();
            ChangeNameResponse result = proxy.ChangeName(new ChangeNameRequest(folderId,newFolderName));
            proxy.Close();
            return result.ChangeNameResult;
        }


        public static bool DeleteFolder(int folderId)
        {
            StreamingServiceClient proxy = new StreamingServiceClient();
            DeleteFileResponse result = proxy.DeleteFile(new DeleteFileRequest(folderId));
            proxy.Close();
            return result.DeleteFileResult;
        }


        public static FolderInfo GetFolder(int folderId)
        {
            StreamingServiceClient proxy = new StreamingServiceClient();
            GetFolderResponse result = proxy.GetFolder(new GetFolderRequest(folderId));
            proxy.Close();
            return new FolderInfo() 
            { 
                Id = result.GetFolderResult.Id, FolderName = result.GetFolderResult.FolderName, 
                IsPublic = result.GetFolderResult.IsPublic,
                Owner = result.GetFolderResult.Owner,
                Path = result.GetFolderResult.Path,
                ParentId = result.GetFolderResult.ParentId
            };
        }


        public static List<FolderInfo> GetFolders(int parentId, string owner, int sortBy, bool isSortAscending, int pageSize, int pageIndex)
        {
            List<FolderInfo> lst = new List<FolderInfo>();
            StreamingServiceClient proxy = new StreamingServiceClient();
            GetFoldersResponse result = proxy.GetFolders(new GetFoldersRequest(parentId,owner,sortBy,isSortAscending,pageSize,pageIndex));
            proxy.Close();
            foreach (Folder f in result.GetFoldersResult)
            {
                lst.Add(new FolderInfo() { Id = f.Id, FolderName = f.FolderName, IsPublic = f.IsPublic, Owner = f.Owner, Path = f.Path, ParentId = f.ParentId });
            }
            return lst;
        }

        public static List<System.IO.DirectoryInfo> GetPhysicalFolders(int parentId)
        {
            List<System.IO.DirectoryInfo> dirs = new List<System.IO.DirectoryInfo>();
            StreamingServiceClient proxy = new StreamingServiceClient();
            dirs = proxy.GetPhysicalFolders(new GetPhysicalFoldersRequest(parentId)).GetPhysicalFoldersResult;
            proxy.Close();
            return dirs;
        }
       
    }
}
